package cn.hchaojie.lucene;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

public class LuceneSearcher {
	private static final String INDEX_PATH = "D:\\workspace\\workspace_stxy\\lucene-day01\\index";
	
	public static void main(String[] args) throws IOException {
		IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_PATH)));
		IndexSearcher searcher = new IndexSearcher(reader);

		// Analyzer analyzer = new StandardAnalyzer();	
		
		TopDocs topDocs = searcher.search(new TermQuery(new Term("content", "apache")), 10);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc d : scoreDocs) {
			int id = d.doc;	// 每一条记录的id（理解数据库里面的主键）
			Document doc = searcher.doc(id);
			
			String name = doc.get("name");
			String path = doc.get("path");
			String size = doc.get("size");
			String content = doc.get("content");
			
			String msg = "name: %s path: %s size:%s content:%s \n";
			System.out.printf(msg, name, path, size, content);
		}
		
	}
}
